package com.example.api.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Jasypt加密配置类
 * 用于配置数据库密码加密
 * 
 * @author example
 * @since 1.0.0
 */
@Configuration
public class JasyptConfig {

    /**
     * 配置Jasypt字符串加密器
     * 
     * @return 字符串加密器
     */
    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        
        // 设置加密密钥，建议通过环境变量获取
        config.setPassword(System.getenv().getOrDefault("JASYPT_ENCRYPTOR_PASSWORD", "esun@123"));
        
        // 设置加密算法
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        
        // 设置密钥获取迭代次数
        config.setKeyObtentionIterations("1000");
        
        // 设置连接池大小
        config.setPoolSize("1");
        
        // 设置提供者名称
        config.setProviderName("SunJCE");
        
        // 设置盐生成器类名
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        
        // 设置IV生成器类名
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        
        // 设置字符串输出类型
        config.setStringOutputType("base64");
        
        encryptor.setConfig(config);
        return encryptor;
    }
} 